﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using BaHuan.BusinessLayer;
using BaHuan.Entities;
using BaHuan.Common;

namespace BaHuan
{
    public partial class FrmQLKhayDung : Form
    {
        private KhayDungManagement khaydungMgt = new KhayDungManagement();

        private bool isEdit = false;

        public FrmQLKhayDung()
        {
            InitializeComponent();
        }

        private void FrmQLKhayDung_Load(object sender, EventArgs e)
        {
            BindingToGrid();
            btnLuu.Enabled = false;
            btnSua.Enabled = false;
            ChangeTextBoxStatus(false);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            isEdit = true;
            groupBox1.Enabled = true;
            btnLuu.Enabled = true;
            txtTen.Select();
            ChangeTextBoxStatus(true);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtMaSo.Text) && !string.IsNullOrEmpty(txtTen.Text) && !string.IsNullOrEmpty(txtSoHop.Text) && !string.IsNullOrEmpty(txtSoTrung.Text))
            {
                ResultObj result = new ResultObj();

                string sTen = txtTen.Text;
                long lSoHop = Convert.ToInt64(txtSoHop.Text);
                long lSoTrung = Convert.ToInt64(txtSoTrung.Text);

                KhayDung khaydung = new KhayDung();
                khaydung.Ten = sTen;
                khaydung.MaSo = txtMaSo.Text;
                khaydung.SoLuongHop = lSoHop;
                khaydung.SoLuongTrung = lSoTrung;

                if (!isEdit)
                {
                    result = khaydungMgt.SaveNewKhayDung(khaydung);
                    khaydung.Id = result.ReturnCode;
                }
                else
                {
                    khaydung.Id =Convert.ToInt32(lblId.Text);
                    result = khaydungMgt.UpdateKhayDung(khaydung);
                    isEdit = false;
                }

                if (result.ReturnCode > 0)
                {
                    MessageBox.Show(" Lưu thành công", "Thông báo");

                    btnSua.Enabled = false;
                    btnLuu.Enabled = false;
                    groupBox1.Enabled = false;
                    ClearTextBox();
                }
                else if (result.ReturnCode == Definitions.DUPLICATED_VALUE)
                {
                    MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
                    txtTen.Select();
                }
                else
                {
                    MessageBox.Show("Thất bại");
                    txtTen.Select();
                }

                BindingToGrid();
            }
            else
            {
                MessageBox.Show("Chưa điền đầy đủ thông tin của khay đựng", "Thông báo");
                return;
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            isEdit = false;
            ClearTextBox();
            groupBox1.Enabled = true;
            btnLuu.Enabled = true;
            btnSua.Enabled = false;
            ChangeTextBoxStatus(true);
        }

        private void dgvList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            btnLuu.Enabled = false;
            btnSua.Enabled = true;
            groupBox1.Enabled = false;
            if (e.RowIndex > -1)
            {
                lblId.Text = dgvList.Rows[e.RowIndex].Cells[4].Value.ToString();
                txtMaSo.Text = dgvList.Rows[e.RowIndex].Cells[0].Value.ToString();
                txtTen.Text = dgvList.CurrentRow.Cells[1].Value.ToString();
                txtSoHop.Text = dgvList.CurrentRow.Cells[2].Value.ToString();
                txtSoTrung.Text = dgvList.CurrentRow.Cells[3].Value.ToString();
            }
        }

        private void txtSoHop_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Vui lòng nhập số!");
                e.Handled = true;
            }
        }

        private void txtSoTrung_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Vui lòng nhập số!");
                e.Handled = true;
            }
        }

        private void ChangeTextBoxStatus(bool isEnable)
        {
            txtMaSo.Enabled = isEnable;
            txtTen.Enabled = isEnable;
            txtSoHop.Enabled = isEnable;
            txtSoTrung.Enabled = isEnable;
        }

        private void ClearTextBox()
        {
            txtMaSo.Clear();
            txtTen.Clear();
            txtSoHop.Clear();
            txtSoTrung.Clear();
        }

        private void BindingToGrid()
        {
            dgvList.AutoGenerateColumns = false;
            dgvList.DataSource = GetAllKhayDung();
        }

        public List<KhayDung> GetAllKhayDung()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = khaydungMgt.GetAllKhayDung();
            List<KhayDung> listKhayDung = (List<KhayDung>)resultObj.Result;

            return listKhayDung;
        }
    }
}
